
namespace Nutils
{
    public class Helper
    {
        public delegate void SetString(string s);
        public string About;

        private System.Windows.Forms.ToolTip _toolTip;
        private SetString _setString;
        private ControlDataDict _controls;

        public Helper(string sHelpFile, SetString setFunc, System.ComponentModel.IContainer components)
        {
            this._toolTip = new System.Windows.Forms.ToolTip(components);
            this._toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            this._toolTip.Popup += new System.Windows.Forms.PopupEventHandler(this._ToolTip_Popup);

            _setString = new SetString(setFunc);
            About = "";
            _controls = new ControlDataDict();
            ReadConfig(sHelpFile);
        }

        public void RegisterControl(System.Windows.Forms.Control control)
        {
            if (!_controls.ContainsKey(control.Name))
            {
                Nutils.Console.Spam("No help info for: " + control.Name);
                return;
            }

            string shortTip = _controls[control.Name].ShortTip;
            if (0 >= shortTip.Length)
            {
                _toolTip.SetToolTip(control, ":Dummy Tip:");
                return;
            }

            _toolTip.SetToolTip(control, shortTip);
        }

        private void _ToolTip_Popup(object sender, System.Windows.Forms.PopupEventArgs e)
        {
            if (!_controls.ContainsKey(e.AssociatedControl.Name))
            {
                return;
            }

            ControlData data = _controls[e.AssociatedControl.Name];
            _setString(data.LongTip);

            if( 0 >= data.ShortTip.Length )
            {
                e.Cancel = true;
            }
        }

        public void ReadConfig(string configFile)
        {
            if (!System.IO.File.Exists(configFile))
                return;

            XML.Tree xmlTree = XML.Parse(configFile);
            if (null == xmlTree)
            {
                return;
            }

            // HelpTips
            foreach (XML.Tree node in xmlTree.ForEach("HelpTip"))
            {
                ControlData controlData = new ControlData();
                controlData.Parse(xmlTree);

                if (!_controls.ContainsKey(controlData.ControlName))
                    _controls.Add(controlData.ControlName, controlData);
                else
                    _controls[controlData.ControlName] = controlData;
            }

            // Everything else
            About = xmlTree.FindTextValue("About", About);
        }

        class ControlData
        {
            public string ControlName;
            public string ShortTip;
            public string LongTip;

            public ControlData()
            {
                ControlName = "";
                ShortTip = "";
                LongTip = "";
            }

            public void Parse(XML.Tree xmlTree)
            {
                ControlName = xmlTree.FindTextValue( "ControlName", ControlName );
                ShortTip = xmlTree.FindTextValue( "Short", ShortTip );
                LongTip = xmlTree.FindTextValue( "Long", LongTip );
            }
        }
        class ControlDataDict : System.Collections.Generic.Dictionary<string, ControlData> { }
    }
}















